// Add a request interceptor
import axios from "axios";
import store from "@/store";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { getToken } from "@/utils/auth";

axios.defaults.withCredentials = true;

const filterNoTokenArr = ["/user/login", "/user/register"];

axios.interceptors.request.use(
  function (config) {
    // console.log(store.getters.token);
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const uri = config.url.substring(25);
    console.log(uri);
    let flag = true;
    filterNoTokenArr.forEach((item) => {
      if (item === uri) {
        flag = false;
        return;
      }
    });
    if (flag) {
      config.headers["token"] = getToken();
    }
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);
